<!-- 销售出库管理 -->
<template>
  <div class="mod-config">
    <el-button-group style="margin-bottom: 10px;">
      <el-button icon="el-icon-search" type="primary" @click="searchHandle">查询</el-button>
      <el-button icon="el-icon-delete" type="primary" @click="searchReset">清空</el-button>
      <el-button v-if="permissions.eims_takeinventory_add" icon="el-icon-plus" type="primary" @click="addOrUpdateHandle()">新增</el-button>
    </el-button-group>
    <el-form inline label-width="100px" @keyup.enter.native="searchHandle">
      <el-form-item label="单据编号">
        <el-input v-model="dataForm.takeBillId"></el-input>
      </el-form-item>
      <el-form-item label="单据日期">
        <el-row>
          <el-col :span="11">
            <el-date-picker style="width: 100%;" v-model="dataForm.createTimeStart" value-format="yyyy-MM-dd" type="date"
              placeholder="选择日期"></el-date-picker>
          </el-col>
          <el-col :span="2" style="text-align: center;">至</el-col>
          <el-col :span="11">
            <el-date-picker style="width: 100%;" v-model="dataForm.createTimeEnd" type="date" value-format="yyyy-MM-dd"
              placeholder="选择日期"></el-date-picker>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="审核状态">
        <el-select v-model="dataForm.isAudit" placeholder="请选择审核状态">
          <el-option label="未审核" value="0"></el-option>
          <el-option label="已审核" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="销售部门">
        <el-select v-model="dataForm.receptionId" clearable>
          <el-option v-for="item in receptionOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发出仓位">
        <el-select v-model="dataForm.outStockId" clearable>
          <el-option v-for="item in stockOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="dataForm.memo"></el-input>
      </el-form-item>
      <el-form-item label="商品代码">
        <el-input v-model="dataForm.goodsCode"></el-input>
      </el-form-item>
      <el-form-item label="商品名称">
        <el-input v-model="dataForm.goodsName"></el-input>
      </el-form-item>
    </el-form>
    <div class="avue-crud">
      <el-table :data="dataList" border v-loading="dataListLoading" :max-height="tableHeight">
        <el-table-column prop="takeBillId" label="单据编号" width="150"></el-table-column>
        <el-table-column prop="createTime" label="单据日期" width="160"></el-table-column>
        <el-table-column prop="deptName" label="销售部门" width="210"></el-table-column>
        <el-table-column prop="isAudit" label="审核状态" :formatter="isAuditType" width="90"></el-table-column>
        <el-table-column prop="createUsername" label="制单人" width="90"></el-table-column>
        <el-table-column prop="auditUsername" label="审核人" width="90"></el-table-column>
        <el-table-column prop="memo" label="备注" show-overflow-tooltip></el-table-column>
        <el-table-column label="操作" fixed="right" width="210">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-search" @click="showDetails(scope.row.id)" :disabled="scope.row.isAudit == 0">详情</el-button>
            <el-button v-if="permissions.eims_takeinventory_edit" type="text" icon="el-icon-edit" @click="audit(scope.row.id)"
              :disabled="scope.row.isAudit == 1">审核</el-button>
            <el-button v-if="permissions.eims_takeinventory_edit" type="text" icon="el-icon-edit" @click="addOrUpdateHandle(scope.row.id)"
              :disabled="scope.row.isAudit == 1">修改</el-button>
            <el-button v-if="permissions.eims_takeinventory_del" type="text" icon="el-icon-delete" @click="deleteHandle(scope.row.id)"
              :disabled="scope.row.isAudit == 1">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="avue-crud__pagination">
      <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next"></el-pagination>

    </div>
    <detail-table v-if="detailsVisible" ref="details" @refreshDataList="searchHandle"></detail-table>
    <add-table v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="searchHandle"></add-table>
    <supply-search v-if="supplyVisible" ref="supplySearch" @refreshData="updateSearchForm"></supply-search>
    <search-brand v-if="brandVisible" ref="brandSearch" @refreshData="updateBrandForm"></search-brand>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { filterParams } from '@/util/tools'
  import { getStockList } from '@/api/eims/stock'
  import { getDeptInfoByDeptType } from "@/api/admin/dept"
  import {
    fetchList,
    auditRecipientsOfOutbound,
    delObj
  } from '@/api/eims/take-inventory/eimstakeinventory.js'


  import detailTable from "./details"
  import AddTable from "./add"

  export default {
    data() {
      return {
        dataForm: {
          supplyName: '',
          brandName: '',
          createTimeStart: '',
          createTimeEnd: ''
        },
        receptionTypeOptions: [{
            label: '领用部门',
            value: 1
          },
          {
            label: '客户领用',
            value: 2
          }
        ],
        stockOptions: [],
        receptionOptions: [],
        isAuditOptions: [{
            label: '已审核',
            value: 1
          },
          {
            label: '未审核',
            value: 0
          }
        ],
        dataList: [],
        searchShow: true,
        pageIndex: 1,
        pageSize: 100,
        totalPage: 0,
        dataListLoading: false,
        addOrUpdateVisible: false,
        detailsVisible: false,
        supplyVisible: false,
        brandVisible: false,
      }
    },
    components: {
      // TableForm,
      detailTable,
      AddTable
    },
    created() {
      this.getDataList()
      this.stockList()
      this.receptionList()
    },
    computed: {
      ...mapGetters(['permissions']),
      tableHeight: function(){
        return window.innerHeight - 260
      }
    },
    methods: {
      searchHandle(){
        this.pageIndex = 1
        this.getDataList()
      },
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        let params1 = filterParams(this.dataForm)
        let params2 = {
          current: this.pageIndex,
          size: this.pageSize
        };
        fetchList(Object.assign(params1, params2)).then(response => {
          if(response.data.code == '0'){
            this.dataList = response.data.data.records
            this.totalPage = response.data.data.total
          }else{
            this.$message.error(response.data.msg)
          }
          this.dataListLoading = false
        })
      },
      updateSearchForm(data) {
        this.dataForm.supplyId = data.id
        this.dataForm.supplyName = data.name

      },
      updateBrandForm(data) {
        this.dataForm.brandId = data.id
        this.dataForm.brandName = data.name
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle(id) {
        this.$confirm('是否确认删除ID为' + id, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delObj(id)
        }).then(response => {
          if(response.data.code == '0'){
            this.$message.success('删除成功')
            this.getDataList()
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      searchChange() {
        this.pageIndex = 1;
        this.getDataList()
      },
      // 搜索清空
      searchReset() {
        this.$refs.searchForm.resetFields();
        this.getDataList(this.page)
      },
      handleSearchShow() {
        this.searchShow = !this.searchShow;
      },
      /**
       * 刷新回调
       */
      refreshChange() {
        this.getDataList(this.page)
      },
      //  获取仓位列表
      stockList() {
        getStockList().then(response => {
          if(response.data.code == '0'){
            this.stockOptions = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      //  获取部门列表
      receptionList() {
        getDeptInfoByDeptType(1).then(response => {
          if(response.data.code == '0'){
            this.receptionOptions = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      //清空查询列表
      searchReset() {
        this.dataForm = [];
      },
      // 查看详情
      showDetails(id) {
        this.detailsVisible = true;
        this.$nextTick(() => {
          this.$refs.details.init(id);
        });
      },
      //审核
      audit(id) {
        this.$confirm('是否审核ID为' + id, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return auditRecipientsOfOutbound(id)
        }).then(response => {
          if(response.data.code == '0'){
            this.$message.success('审核成功')
            this.getDataList()
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      isAuditType(row, column, cellValue, index) {
        switch (cellValue) {
          case 0:
            return '未审核'
            break
          case 1:
            return '已审核'
            break
        }
      },
      chooseSupply() {
        this.supplyVisible = true
        this.$nextTick(() => {
          this.$refs.supplySearch.init()
        })
      },
    }
  }
</script>
